import axios from 'axios'
import { ElMessage } from 'element-plus'

// 创建专门的认证请求实例，不依赖token
const authService = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL || '',
    timeout: 30000,
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
})

// 响应拦截器
authService.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.error('Auth request error:', error)
        
        let message = '网络错误'
        
        if (error.response) {
            const { status, data } = error.response
            
            switch (status) {
                case 400:
                    message = data.message || data || '请求参数错误'
                    break
                case 401:
                    message = data.message || data || '用户名或密码错误'
                    break
                case 500:
                    message = data.message || '服务器内部错误'
                    break
                default:
                    message = data.message || data || `连接错误${status}`
            }
        } else if (error.code === 'ECONNABORTED') {
            message = '请求超时'
        } else if (error.message.includes('Network Error')) {
            message = '网络连接异常'
        }
        
        ElMessage.error(message)
        return Promise.reject(error)
    }
)

export default authService
